﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using K.EnterpriseLibary;
using IBatisNet.DataAccess;
using K.Persistence.Interface;
using K.Persistence.MapperDao;
using K.EnterpriseLibary.Entity;
using System.Data;
using K.Contract;
using System.Text.RegularExpressions;
using K.EnterpriseLibary.Config;
using K.EnterpriseLibary.Common;
namespace K.Service
{
    public class UserService : IUserService
    {

        private IUser dal;
        public UserService()
        {
            dal = DataAccess.CreateUser();
        }



        /// <summary>
        /// 检查用户是否存在
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="LoginType">登录类型</param>
        /// <returns></returns>
        public bool CheckUserExsit(string inputStr, int LoginType)
        {
            List<UserInfo> listUserInfo = new List<UserInfo>();
            switch (LoginType)
            {
                case 0: listUserInfo = dal.SelectUserInfoByAnyProperty(inputStr);
                    break;
                case 1: listUserInfo = dal.SelectUserInfoByAnyProperty(inputStr);
                    break;
                case 2: listUserInfo = dal.SelectUserInfoByAnyProperty(inputStr);
                    break;
            }
            if (listUserInfo.Count > 0)
            {
                return true;
            }
            return false;

        }

        public EnumRegister Register(UserInfo userInfo, UserBaseInfo userBaseInfo, out int uid)
        {
            dal.CreateUserInfo(userInfo);
            uid = userInfo.UserID;
            return EnumRegister.Succeed;


        }


        /// <summary>
        /// 根据ID找用户
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public UserInfo GetUserInfo(int userId)
        {
            return dal.SelectUserInfoByPrimaryKey(userId);
        }

        #region
        /*
                /// <summary>
        /// 是否是管理员
        /// </summary>
        /// <param name="userInfo">会员实例</param>
        /// <returns></returns>
        public bool IsAdmin(UserInfo userInfo)
        {
            if(userInfo.IsAdmin > 0)
                return true;
            return false;
        }

        /// <summary>
        /// 检查用户ID是否存在
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool CheckUserExist(int userId)
        {
            UserInfo userInfo = new UserInfo();
            userInfo = dal.SelectUserInfoByPrimaryKey(userId);
            if (!userInfo.IsNull())
            {
                return true;
            }
            return false;
        }

        

        /// <summary>
        /// 检查用户返回登录状态
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="loginNum">登录状态</param>
        /// <returns></returns>
        public EnumLoginState CheckUser(UserInfo userInfo, out int loginNum)
        {
            
            int lNum = 0;
            string pwd = string.Empty;
            EnumUserState us = EnumUserState.Lock; //默认为锁定状态
            bool exist = false;
            UserInfo userInfoSelectById = new UserInfo();
            userInfoSelectById = dal.SelectUserInfoByPrimaryKey(userInfo.UserID);
            if (!userInfoSelectById.IsNull())
            {
                pwd = userInfoSelectById.Password;
                us = (EnumUserState)userInfoSelectById.State; //找到该用户的状态
                exist = true;
                lNum = userInfoSelectById.LoginTimes;
            }

            loginNum = lNum;
            if (!exist || pwd == string.Empty || pwd != userInfo.Password)
            {
                return EnumLoginState.Err_NameOrPwdError;
            }
            if (us == EnumUserState.Register) //刚注册后的状态
            {
                if (UiConfig.RegVer == "1") //是否需要邮件验证后才能登录
                {
                    return EnumLoginState.Err_UnActivation; //未激活
                }
            }
            else if (us == EnumUserState.Lock) //锁定状态
            {
                return EnumLoginState.Err_Locked; //用户被锁定
            }
            //此处省去登录之星的代码
            //此处省去每登录一次就增加积分或金币的代码
            userInfoSelectById.LastLoginTime = DateTime.Now;
            dal.UpdateUserInfo(userInfoSelectById);
            return EnumLoginState.Succeed;

        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>用户</returns>
        public UserInfo GetUserInfoById(UserInfo userInfo)
        {
            UserInfo userInfoSelectById = new UserInfo();
            userInfoSelectById = dal.SelectUserInfoByPrimaryKey(userInfo.UserID);
            return userInfoSelectById;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>true:更新成功;</returns>
        public bool ChangeUserInfo(UserInfo userInfo)
        {
            dal.UpdateUserInfo(userInfo);
            return true;//如何得知更新成功？
         * 
            SqlParameter param = new SqlParameter("@Password", SqlDbType.NVarChar, 32);
            param.Value = Password;
            string sql = "update NT_User set Password=@Password where UserID=" + UserID + "";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param) > 0 ? true : false;
        *
        }

        /// <summary>
        /// 更新邮箱
        /// </summary>
        /// <param name="userInfo">更新邮箱</param>
        /// <param name="loginType">邮箱参数</param>
        /// <returns></returns>
        public bool ChangeEmail(UserInfo userInfo,int loginType)
        {
            //检查该邮箱下是否存在用户
            if (!CheckUserExsit(userInfo.Email, loginType))
            {
                if (ChangeUserInfo(userInfo))
                    return true;
            }
            return false;
        }
         */
        #endregion
    }
}
